package org.example;


import com.alibaba.fastjson2.JSONObject;

import java.io.*;
import java.nio.charset.Charset;

public class MergeBilibliCache {

    static String path = "C:\\Users\\user\\Desktop\\6643205";
    static String outputPath = "C:\\Users\\user\\Desktop\\output";
    static String baseCommand="ffmpeg -i \"%s\" -i \"%s\" -vcodec copy -acodec copy \"%s\"";//注意路径要加上引号！不然遇到有空格的文件名或者路径教你做人
    public static void main(String[] args) {
        //要遍历的路径
        File file = new File(path);        //获取其file对象
        File output=new File(outputPath);
        if(!output.exists()){
            output.mkdirs();
        }

        try {
            func(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void func(File file) throws IOException {

        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory())    //若是目录，则递归打印该目录下的文件
            {
                System.out.println("当前位于" + f.getAbsolutePath());
                converter(f.getAbsolutePath());
            }
            if (f.isFile())        //若是文件，直接打印
                System.out.println("当前发现" + f);
        }
    }

    public static void converter(String path) throws IOException {
        File file = new File(path + "\\entry.json");
//        FileInputStream inputStream=new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String builder = null;
        builder = bufferedReader.readLine();
        JSONObject jsonObject = JSONObject.parseObject(builder,null);
        String name=jsonObject.getJSONObject("page_data").getString("part");
        String type_tag=jsonObject.getString("type_tag");
        System.out.println(type_tag);
        System.out.println(jsonObject.getJSONObject("page_data").get("part"));
        String newCommand=String.format(baseCommand,path+"\\"+type_tag+"\\video.m4s",path+"\\"+type_tag+"\\audio.m4s",outputPath+"\\"+name+ Math.random()+".mp4");
        execCommand(newCommand);


    }
    //利用文件来缓存，不然很容易导致阻塞
    public static void execCommand(String command) {
        BufferedReader br = null;
        try {
            File file = new File("daemonTmp");
            File tmpFile = new File("daemonTmp\\temp.tmp");//新建一个用来存储结果的缓存文件
            if (!file.exists()) {
                file.mkdirs();
            }
            if (!tmpFile.exists()) {
                tmpFile.createNewFile();
            }
            ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", command).inheritIO();
            pb.redirectErrorStream(true);//这里是把控制台中的红字变成了黑字，用通常的方法其实获取不到，控制台的结果是pb.start()方法内部输出的。
            pb.redirectOutput(tmpFile);//把执行结果输出。
            pb.start().waitFor();//等待语句执行完成，否则可能会读不到结果。
            InputStream in = new FileInputStream(tmpFile);
            br = new BufferedReader(new InputStreamReader(in, Charset.forName("GBK")));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            br = null;
            tmpFile.delete();//卸磨杀驴。
            System.out.println("执行完成");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
